package com.ruoyi.canteen.controller;

import java.util.List;

import com.ruoyi.canteen.domain.CanteenMd;
import com.ruoyi.canteen.service.ICanteenMdService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.canteen.domain.CanteenCpClass;
import com.ruoyi.canteen.service.ICanteenCpClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品分类Controller
 * 
 * @author ruoyi
 * @date 2021-09-08
 */
@RestController
@RequestMapping("/canteen/class")
public class CanteenCpClassController extends BaseController
{
    @Autowired
    private ICanteenCpClassService canteenCpClassService;

    @Autowired
    private TokenService tokenService;


    @Autowired
    private ICanteenMdService canteenMdService;

    /**
     * 查询菜品分类列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(CanteenCpClass canteenCpClass)
    {
        startPage();
        List<CanteenCpClass> list = canteenCpClassService.selectCanteenCpClassList(canteenCpClass);
        return getDataTable(list);
    }

    /**
     * 导出菜品分类列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:export')")
    @Log(title = "菜品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CanteenCpClass canteenCpClass)
    {
        List<CanteenCpClass> list = canteenCpClassService.selectCanteenCpClassList(canteenCpClass);
        ExcelUtil<CanteenCpClass> util = new ExcelUtil<CanteenCpClass>(CanteenCpClass.class);
        return util.exportExcel(list, "菜品分类数据");
    }

    /**
     * 获取菜品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(canteenCpClassService.selectCanteenCpClassById(id));
    }

    /**
     * 新增菜品分类
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:add')")
    @Log(title = "菜品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CanteenCpClass canteenCpClass)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CanteenMd md = null;
        if(loginUser.getDeptId() != null)
        {
            canteenCpClass.setDeptId(loginUser.getDeptId());
            //获取门店id
            md =  canteenMdService.selectCanteenMdByDeptId(loginUser.getDeptId());
            if(md != null)
                canteenCpClass.setMdId(md.getMdId());
        }

        return toAjax(canteenCpClassService.insertCanteenCpClass(canteenCpClass));
    }

    /**
     * 修改菜品分类
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:edit')")
    @Log(title = "菜品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CanteenCpClass canteenCpClass)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CanteenMd md = null;
        if(loginUser.getDeptId() != null)
        {
            canteenCpClass.setDeptId(loginUser.getDeptId());
            //获取门店id
            md =  canteenMdService.selectCanteenMdByDeptId(loginUser.getDeptId());
            if(md != null)
                canteenCpClass.setMdId(md.getMdId());
        }

        return toAjax(canteenCpClassService.updateCanteenCpClass(canteenCpClass));
    }

    /**
     * 删除菜品分类
     */
    @PreAuthorize("@ss.hasPermi('canteen:class:remove')")
    @Log(title = "菜品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(canteenCpClassService.deleteCanteenCpClassByIds(ids));
    }
}
